package com.jingyanzi.resallocation.service.impl;

import java.util.Calendar;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jingyanzi.resallocation.dao.ResClubMapper;
import com.jingyanzi.resallocation.domain.ClubDO;
import com.jingyanzi.resallocation.entity.ResClub;
import com.jingyanzi.resallocation.entity.ResClubExample;
import com.jingyanzi.resallocation.entity.ResClubExample.Criteria;
import com.jingyanzi.resallocation.query.ClubQueryDO;
import com.jingyanzi.resallocation.result.ResultDO;
import com.jingyanzi.resallocation.result.ResultSupport;
import com.jingyanzi.resallocation.service.IClubService;
import com.jingyanzi.resallocation.util.BeanUtilsExtends;

@Service("clubService")
public class ClubServiceImpl extends BaseServiceImpl implements IClubService {

	@Autowired
	private ResClubMapper resClubMapper;

	public ResultDO add(ClubDO clubDO) {
		ResClub club = new ResClub();

		ResultSupport result = BeanUtilsExtends.copy(club, clubDO);
		if (!result.isSuccess()){
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "对象转换失败");
			result.setSuccess(false);
			return result;
		}
		club.setCreatetime(Calendar.getInstance().getTime());
		club.setIsDelete(1);
		int r = -1;

		try {
			r = resClubMapper.insert(club);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加场馆信息失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.CLUB_GET_ERROR);
			result.setErrorMsg("添加场馆信息失败");
			result.setSuccess(false);
			return result;
		}
		return result;
	}

	public ResultDO edit(ClubDO clubDO) {
		ResClub club = new ResClub();

		ResultSupport result = BeanUtilsExtends.copy(club, clubDO);
		if (!result.isSuccess())
			return result;
		int r = -1;

		try {
			r = resClubMapper.updateByPrimaryKeySelective(club);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "修改场馆信息失败");
			result.setSuccess(false);
			return result;
		}

		if (r < 1) {
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "添加场馆信息失败");
			result.setSuccess(false);
			return result;
		}

		return result;
	}

	public ResultDO del(Integer[] id) {
		ResClub club = new ResClub();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		for (int i = 0; i < id.length; i++) {
			club.setId(id[i]);
			club.setIsDelete(-1);
			int r = -1;

			try {
				r = resClubMapper.updateByPrimaryKeySelective(club);
			} catch (Exception e) {
				e.printStackTrace();
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除场馆信息失败");
				result.setSuccess(false);
				return result;
			}

			if (r < 1) {
				result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
				result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "删除场馆信息失败");
				result.setSuccess(false);
				return result;
			}
		}
		return result;
	}

	public ResultDO get(Integer id) {
		ResClub club = new ResClub();

		ResultSupport result = new ResultSupport();
		if (!result.isSuccess())
			return result;
		try {
			club = resClubMapper.selectByPrimaryKey(id);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取场馆信息失败");
			result.setSuccess(false);
			return result;
		}

		if (club == null) {
			result.setErrorCode(ResultDO.CLUB_GET_ERROR);
			result.setErrorMsg("不存在该场馆信息");
			result.setSuccess(false);
			return result;
		}

		ClubDO clubDO = getClubDO(club);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, clubDO);
		return result;
	}

	public ResultDO query(ClubQueryDO clubQueryDO) {
		ResultSupport result = new ResultSupport();

		List<ResClub> list = null;

		ResClubExample example = new ResClubExample();

		Criteria createCriteria = example.createCriteria();
		if (clubQueryDO.getType() != null && clubQueryDO.getType() != 0) {
			createCriteria.andTypeEqualTo(clubQueryDO.getType());
		}
		if (clubQueryDO.getLevel() != null && clubQueryDO.getLevel() != 0) {
			createCriteria.andLevelEqualTo(clubQueryDO.getLevel());
		}
		createCriteria.andIsDeleteEqualTo(1);
		List<ClubDO> clubDOs = null;

		if (clubQueryDO.isPage()) {
			example.setLimitByPage(clubQueryDO.getStart() + "," + clubQueryDO.getPageRows());
			long count = resClubMapper.countByExample(example);
			result.setModel(ResultSupport.SECOND_MODEL_KEY, count);
		}

		try {
			list = resClubMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取场馆信息失败");
			result.setSuccess(false);
			return result;
		}
		clubDOs = getClubDOList(list);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, clubDOs);
		return result;
	}

	public ResultDO select(ClubQueryDO clubQueryDO) {
		ResultSupport result = new ResultSupport();

		List<ResClub> list = null;

		ResClubExample example = new ResClubExample();

		Criteria createCriteria = example.createCriteria();
		if (clubQueryDO.getType() != null && clubQueryDO.getType() != 0) {
			createCriteria.andTypeEqualTo(clubQueryDO.getType());
		}
		if (clubQueryDO.getLevel() != null && clubQueryDO.getLevel() != 0) {
			createCriteria.andLevelEqualTo(clubQueryDO.getLevel());
		}
		createCriteria.andIsDeleteEqualTo(1);
		List<ClubDO> clubDOs = null;
		try {
			list = resClubMapper.selectByExample(example);
		} catch (Exception e) {
			e.printStackTrace();
			result.setErrorCode(ResultDO.SYSTEM_EXCEPTION_ERROR);
			result.setErrorMsg(ResultDO.SYSTEM_EXCEPTION_ERROR_MSG + "获取场馆信息失败");
			result.setSuccess(false);
			return result;
		}
		clubDOs = getClubDOList(list);
		result.setModel(ResultSupport.FIRST_MODEL_KEY, clubDOs);
		return result;
	}

}
